<template>
  <el-drawer :title="title" :visible.sync="visible" @close="onClose" size="35%" :wrapperClosable="false"
    :close-on-press-escape="false" append-to-body>
    <el-form v-loading="loading" ref="form" :model="form" :disabled="disabled" label-width="auto" class="form">
      <el-form-item label="店铺账户" prop="shopId" :rules="[{ required: true, message: '请选择店铺账户', trigger: 'change' }]">
        <el-select @change="$forceUpdate()" filterable v-model="form.shopId" placeholder="请选择店铺账户" clearable
          style="width: 240px">
          <el-option v-for="item in shopList" :key="item.id" :label="item.nickName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属代运营">
        <el-input v-model="agency" :disabled="true" />
      </el-form-item>
      <el-form-item label="抽佣费率" prop="feeRate"
        :rules="[{ type: 'number', min: 1, max: 30, required: true, message: '请输入1-30之间的抽佣费率', trigger: 'blur' }]">
        <el-input-number :step="1" :min="0" :max="30" :precision="2" v-model.number="form.feeRate"
          placeholder="请输入费率" />
      </el-form-item>
      <el-divider content-position="left">资质信息</el-divider>
      <el-form-item label="结算名称" prop="shopName" :rules="[{ required: true, message: '请输入结算名称', trigger: 'blur' }]">
        <name-input v-model="form.shopName" placeholder="门店在平台上的结算名称" />
      </el-form-item>
      <el-form-item label="企业名称" prop="shopSignName" :rules="[{ required: true, message: '请输入企业名称', trigger: 'blur' }]">
        <name-input v-model="form.shopSignName" placeholder="营业执照上的企业名称" />
      </el-form-item>
      <el-form-item label="结算卡号" prop="settleCardNo" :rules="[{ required: true, message: '请输入结算卡号', trigger: 'blur' }]">
        <code-input v-model="form.settleCardNo" placeholder="门店在平台上的结算卡" />
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalPersonName"
        :rules="[{ required: true, message: '请输入法人姓名', trigger: 'blur' }]">
        <name-input v-model="form.legalPersonName" placeholder="请输入法人姓名" />
      </el-form-item>
      <el-form-item label="证件类型" prop="licenseType">
        <el-select v-model="form.licenseType">
          <el-option label="身份证" value="ID_CARD" />
        </el-select>
      </el-form-item>
      <el-form-item label="证件号码" prop="licenseNo" :rules="[{ required: true, message: '请输入法人证件号码', trigger: 'blur' }]">
        <code-input v-model="form.licenseNo" /></el-form-item>
      <el-form-item label="联系人" prop="contactPerson"
        :rules="[{ required: true, message: '请输入联系人姓名', trigger: 'blur' }]">
        <name-input v-model="form.contactPerson" />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactMobile"
        :rules="[{ required: true, message: '请输入联系电话', trigger: 'blur' }]">
        <code-input v-model="form.contactMobile" />
      </el-form-item>
    </el-form>
    <div class="footer-operation">
      <el-button @click="visible = false">返回</el-button>
      <template v-if="!disabled && !!form.shopId">
        <el-button :loading="loading" type="success" @click="save" v-clickOnce>保存草稿</el-button>
        <el-button :loading="loading" type="primary" @click="submit" v-hasPermi="['ecs:yeepayWithhold:add']" v-clickOnce>提交</el-button>
      </template>
    </div>
  </el-drawer>
</template>
<script>
import { listShop } from '@/api/ecs/shop'
import { getYeepayWithhold, addYeepayWithhold, updateYeepayWithhold, createYeepayWithhold } from '@/api/ecs/yeepayWithhold'

export default {
  name: 'YeepayWithholdShopEditor',
  data() {
    return {
      visible: false,
      title: '',
      loading: false,
      disabled: false,
      form: {
      },
      shopList: [],
      showResolve: (value) => { },
      showReject: (reason) => { },
    }
  },
  created() {
    listShop({ page: 1, pageSize: 1000000 }).then(res => {
      this.shopList = res.rows
    })
  },
  computed: {
    agency() {
      const shopId = this.form.shopId
      if (!shopId) return ''
      const shop = this.shopList.find(s => s.id == shopId)
      if (!shop) return ''
      return shop.childAgentNickName ?? shop.agentNickName
    }
  },
  methods: {
    show(id, disabled = false) {
      if (id) {
        this.form.id = id
        this.load()
        this.title = '店铺详情'
      } else {
        this.form = {
          id: undefined,
          shopId: undefined,
          merchantNo: '',
          feeRate: undefined,
          shopName: '', shopSignName: '', settleCardNo: '',
          legalPersonName: '', licenseType: 'ID_CARD', licenseNo: '',
          contactPerson: '', contactMobile: '',
        }
        this.title = '新增店铺'
      }
      this.disabled = disabled
      this.visible = true
      return new Promise((resolve, reject) => {
        this.showResolve = resolve
        this.showReject = reject
      })
    },
    onClose() {
      this.$refs.form?.resetFields()
    },
    load() {
      getYeepayWithhold(this.form.id).then(res => {
        this.form = { ...res.data }
      })
    },
    save() {
      const save = (this.form.id) ? updateYeepayWithhold : addYeepayWithhold
      save(this.form).then(res => {
        if (res.code === 200) {
          this.visible = false
          this.showResolve()
        }
      })
    },
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const save = (this.form.id) ? updateYeepayWithhold : addYeepayWithhold;
          save(this.form).then(res => {
            if (res.code === 200) {
              this.form.id = res.data
              return createYeepayWithhold(res.data)
            }
          }).then(res => {
            if (res.code === 200) {
              this.visible = false
              this.showResolve()
            }
          })
        }
      })
    },
  },
}
</script>
<style scoped lang="scss">
.form {
  padding: 20px 20px 80px 20px;
}

.footer-operation {
  bottom: 0;
  width: 100%;
  height: 80px;
  display: flex;
  z-index: 10000;
  padding: 0 20px;
  text-align: center;
  position: absolute;
  align-items: center;
  background-color: #ffffff;
  border-top: 1px solid #EBEEF5;

  .el-button {
    width: 100%;
  }
}
</style>
